package com.sinothk.hussars.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinothk.hussars.base.ErrorCode;
import com.sinothk.hussars.base.entity.PageReq;
import com.sinothk.hussars.entity.*;
import com.sinothk.hussars.mapper.OrgCheckMapper;
import com.sinothk.hussars.mapper.WorkerCheckerMapper;
import com.sinothk.hussars.service.OrgCheckService;
import com.sinothk.hussars.utils.XUtils;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.ResultCode;
import com.sinothk.server.support.utils.IdUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 采集机构表 服务实现类
 * </p>
 *
 * @author liangyt
 * @since 2021-01-05
 */
@Service
public class OrgCheckServiceImpl extends ServiceImpl<OrgCheckMapper, OrgCheckEntity> implements OrgCheckService {

    @Resource
    private OrgCheckMapper mapper;
    @Resource
    private WorkerCheckerMapper workerCheckerMapper;

    @Override
    public OrgCheckEntity add(OrgAddVo orgVo) {
        // 存在验证
        LambdaQueryWrapper<OrgCheckEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrgCheckEntity::getOrgName, orgVo.getOrgName());
        OrgCheckEntity dbOrg = this.getOne(queryWrapper);
        if (dbOrg != null) throw new BizException(ResultCode.DATA_HAS_EXISTED);
        // 验证管理员是否存在
        LambdaQueryWrapper<WorkerCheckerEntity> queryUserWrapper = new LambdaQueryWrapper<>();
        queryUserWrapper.eq(WorkerCheckerEntity::getAccount, orgVo.getManagerAccount());
        WorkerCheckerEntity user = workerCheckerMapper.selectOne(queryUserWrapper);
        if (user == null){// 管理员不存在新增
            WorkerCheckerEntity workerChecker = new WorkerCheckerEntity();
            workerChecker.setId(IdUtil.getIncreaseIdByNanoTime());
            workerChecker.setAccount(orgVo.getManagerAccount());
            workerChecker.setName("管理员");
            Date time = new Date();
            workerChecker.setCreateDate(time);
            workerChecker.setUpdateDate(time);
            workerCheckerMapper.insert(workerChecker);
        }else{
            // 更新管理员密码
            // 更新管理员密码
            WorkerCheckerEntity workerCheckerEntity = new WorkerCheckerEntity();
            workerCheckerEntity.setId(user.getId());
            workerCheckerEntity.setPassword(orgVo.getManagerPwd());
            workerCheckerMapper.updateById(workerCheckerEntity);
        }

        // 查找账号最大的用户520(贵州)300(遵义市)01~(监督机构)1000~1999(检测架构)
        String orgCode = "300011000";
        LambdaQueryWrapper<OrgCheckEntity> queryWrapper4Last = new LambdaQueryWrapper<>();
        queryWrapper4Last.orderByDesc(OrgCheckEntity::getOrgCode);
        List<OrgCheckEntity> dbLastOrg = this.list(queryWrapper4Last);
        if (dbLastOrg != null && dbLastOrg.size() > 0) {
            orgCode = XUtils.coderUtil().getCodeByStart(dbLastOrg.get(0).getOrgCode(), orgCode);
        }
        // 保存
        OrgCheckEntity org = XUtils.classUtil().copyByMethod(orgVo, OrgAddVo.class, OrgCheckEntity.class);
        org.setId(IdUtil.getIncreaseIdByNanoTime());
        org.setOrgCode(orgCode);

        Date time = new Date();
        org.setCreateDate(time);
        org.setUpdateDate(time);

        this.save(org);
        return this.getById(org.getId());
    }

    @Override
    public PageData orgPage(PageReq<OrgCheckEntity> pageVo) {
        Page<OrgCheckEntity> page = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        page.addOrder(OrderItem.desc("create_date"));
        Page<OrgCheckEntity> userIPage = mapper.selectPage(page, Wrappers.<OrgCheckEntity>lambdaQuery().eq(OrgCheckEntity::getState, 0));
        //
        List<OrgCheckEntity> records = userIPage.getRecords();
        PageData pageData = new PageData();
        pageData.setPageNum(userIPage.getCurrent());
        pageData.setPageSize(userIPage.getSize());
        pageData.setCurrent(userIPage.getCurrent());
        pageData.setTotal(userIPage.getTotal());
        pageData.setData(records);
        return pageData;
    }

    @Override
    public OrgCheckEntity edit(OrgEditVo orgVo) {
        // 存在验证
        LambdaQueryWrapper<OrgCheckEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrgCheckEntity::getId, orgVo.getId());
        OrgCheckEntity dbOrg = this.getOne(queryWrapper);
        if (dbOrg == null) throw new BizException(ErrorCode.ORG_NOT_EXISTED);

        // 更新
        OrgCheckEntity org = XUtils.classUtil().copyByMethod(orgVo, OrgEditVo.class, OrgCheckEntity.class);
        org.setUpdateDate(new Date());
        this.updateById(org);

        // 验证管理员是否存在
        LambdaQueryWrapper<WorkerCheckerEntity> queryUserWrapper = new LambdaQueryWrapper<>();
        queryUserWrapper.eq(WorkerCheckerEntity::getAccount, orgVo.getManagerAccount());
        WorkerCheckerEntity user = workerCheckerMapper.selectOne(queryUserWrapper);
        if (user == null) {// 管理员不存在新增
            WorkerCheckerEntity workerManager = new WorkerCheckerEntity();
            workerManager.setId(IdUtil.getIncreaseIdByNanoTime());
            workerManager.setAccount(orgVo.getManagerAccount());
            workerManager.setName("管理员");
            Date time = new Date();
            workerManager.setCreateDate(time);
            workerManager.setUpdateDate(time);
            workerCheckerMapper.insert(workerManager);
        } else {
            // 更新管理员密码
            WorkerCheckerEntity workerCollectEntity = new WorkerCheckerEntity();
            workerCollectEntity.setId(user.getId());
            workerCollectEntity.setPassword(orgVo.getManagerPwd());
            workerCheckerMapper.updateById(workerCollectEntity);
        }

        return this.getById(org.getId());
    }

}
